package com.wgj.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wgj.android.R;
import com.wgj.android.entity.NewHistory;
import com.wgj.android.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7.
 */

public class DescriptionAdapter extends RecyclerView.Adapter<DescriptionAdapter.MyViewHolder> implements View.OnClickListener{
    private List<NewHistory> historys;
    private Context context;
    private LayoutInflater inflater;
    public  DescriptionAdapter(Context context ,List historys){
        this.context = context;
       if(historys==null){
           historys = new ArrayList<NewHistory>();
       }
       this.historys=historys;
       this.inflater = LayoutInflater.from(context);

    }

    private OnItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_new_function,null);
        view.setOnClickListener(this);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.title.setText(historys.get(position).getTitle());
        holder.date.setText(DateUtils.dateForm(historys.get(position).getTime()));
        holder.version.setText(historys.get(position).getVNumber());
        holder.itemView.setTag(position);

    }

    @Override
    public int getItemCount() {
        return historys.size();
    }

    public class MyViewHolder extends  RecyclerView.ViewHolder{
        //private RelativeLayout layout;
        private TextView title;
        private TextView date;
        private TextView version;
        public MyViewHolder(View view) {
            super(view);
            //layout = view.findViewById(R.id.rl);
            //layout.setOnClickListener(this);
            title = view.findViewById(R.id.title);
            date = view.findViewById(R.id.date);
            version = view.findViewById(R.id.versio);
        }

    }
}
